import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {
    public static void main(String[] args) {
//        Calculate calculate = new Calculate(100, 360, 0.049);
//        calculate.principal();
//        calculate.interest();
//        calculate.print();
        JFrame frame = new JFrame("贷款计算器");
        frame.setLayout(new FlowLayout());

        frame.setSize(600,400);

        JLabel labelAmount = new JLabel("贷款总额（万元）");
        JLabel labelPeriod = new JLabel("贷款期数（月）");
        JLabel labelInterest = new JLabel("年利率");

        JTextField textAmount = new JTextField();
        textAmount.setColumns(8);
        JTextField textPeriod = new JTextField();
        textPeriod.setColumns(8);
        JTextField textInterest = new JTextField();
        textInterest.setColumns(8);

        JRadioButton radioBtnAvgInterest = new JRadioButton("等额本息");
        JRadioButton radioBtnAvgCaption = new JRadioButton("等额本金");

        JLabel labelTotalInterest = new JLabel("总利息");
        JTextField textTotalInterest = new JTextField(20);

        JButton buttonCalculate = new JButton("计算");

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioBtnAvgCaption);
        buttonGroup.add(radioBtnAvgInterest);

        frame.add(labelInterest);
        frame.add(textInterest);
        frame.add(labelAmount);
        frame.add(textAmount);
        frame.add(labelPeriod);
        frame.add(textPeriod);

        frame.add(radioBtnAvgCaption);
        frame.add(radioBtnAvgInterest);

        frame.add(labelTotalInterest);
        frame.add(textTotalInterest);

        frame.add(buttonCalculate);

        frame.setVisible(true);

        buttonCalculate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //1.读取数据
                String txtAmount = textAmount.getText();
                String txtPeriod = textPeriod.getText();
                String txtYearInterest = textInterest.getText();
                //2.计算
                Calculate calculate = new Calculate(Integer.parseInt(txtAmount),Integer.parseInt(txtPeriod),Double.parseDouble(txtYearInterest));
                double result;
                if(radioBtnAvgCaption.isSelected()){
                    result = calculate.principal();
                }else{
                    result = calculate.interest();
                }
                //3.输出
                textTotalInterest.setText(String.valueOf(result));
            }
        });


    }
}